package com.framework.loippi.plugin.aliapppay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.framework.loippi.cache.ConfigCache;
import com.framework.loippi.plugin.aliapppay.util.SignUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝app支付
 *
 * @author Administrator
 */
public class AlipayMobilePay {

    public static String toPay(PayCommon payCommon) {
        //AlipayConfig.initAlipayConfig();
        return SignUtils.FormatBizQueryParaMap(getParameterMap(payCommon), false);
    }

    public static Map<String, String> getParameterMap(PayCommon payCommon) {
        HashMap<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("partner", ConfigCache.getConfig("alipay.pid"));
        parameterMap.put("seller_id", "");
        parameterMap.put("out_trade_no", payCommon.getOutTradeNo());
        parameterMap.put("subject", payCommon.getTitle());
        parameterMap.put("body", payCommon.getBody());
        parameterMap.put("total_fee", payCommon.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        parameterMap.put("notify_url", ConfigCache.getConfig("pay.callback.url") + payCommon.getNotifyUrl());
        parameterMap.put("service", "mobile.securitypay.pay");
        parameterMap.put("payment_type", "1");
        parameterMap.put("_input_charset", "utf-8");
        parameterMap.put("it_b_pay", "30m");
        parameterMap.put("return_url", "m.alipay.com");
        try {
            parameterMap.put("sign", URLEncoder.encode(sign(parameterMap, ConfigCache.getConfig("alipay.privateKey")), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        parameterMap.put("sign_type", "RSA");
        return parameterMap;
    }

    private static String sign(HashMap<String, String> parameterMap, String key) {
        String unSignParaString = SignUtils.FormatBizQueryParaMap(parameterMap, false);
        return SignUtils.sign(unSignParaString);
    }


    /**
     * 新版支付宝支付
     *
     * @param payCommon
     * @return
     */
    public static String aliPay(PayCommon payCommon) {
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(ConfigCache.getConfig("alipay.serviceUrl"), ConfigCache.getConfig("alipay.appId"), ConfigCache.getConfig("alipay.privateKey"), "json", AlipayConfig.INPUT_CHARSET,ConfigCache.getConfig("alipay.publicKey"), "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setPassbackParams(payCommon.getBody());
        ;  //描述信息  添加附加数据
        model.setSubject(payCommon.getTitle()); //商品标题
        model.setOutTradeNo(payCommon.getOutTradeNo()); //商家订单编号
        model.setTimeoutExpress("30m"); //超时关闭该订单时间
        model.setTotalAmount(payCommon.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());  //订单总金额
        model.setProductCode("QUICK_MSECURITY_PAY"); //销售产品码，商家和支付宝签约的产品码，为固定值QUICK_MSECURITY_PAY
        request.setBizModel(model);
        request.setNotifyUrl(ConfigCache.getConfig("pay.callback.url") + payCommon.getNotifyUrl());  //回调地址
        String orderStr = "";
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            orderStr = response.getBody();
            System.out.println(orderStr);//就是orderString 可以直接给客户端请求，无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return orderStr;
    }

}
